
local alien = require 'alien'
local config = require 'config'
local gtk = require 'bindings.libgtk'
local gdk = require 'bindings.libgdk'


module (...)


local tlib = alien.load(config.libgtkglext_path)
local dlib = alien.load(config.libgdkglext_path)

local function gtk_Func(name, types)
  local func = tlib["gtk_" .. name]
  func:types(types)
  gtk[name] = func
end

local function gdk_Func(name, types)
  local func = dlib["gdk_" .. name]
  func:types(types)
  gdk[name] = func
end

local function Func(name, types)
  local func = tlib["gtkglext_" .. name]
  func:types(types)
  _M[name] = func
end

local guint = "uint"
local gint = "int"
local gulong = "ulong"
local gboolean = "int"

gtk_Func("gl_parse_args", { ret=gboolean; "pointer" --[[int* argc]], "pointer" --[[char*** argv]]})
gtk_Func("gl_init_check", { ret=gboolean; "pointer" --[[int* argc]], "pointer" --[[char*** argv]]})
gtk_Func("gl_init", { ret="void"; "pointer" --[[int* argc]], "pointer" --[[char*** argv]]})
gtk.GL_DEBUG_MISC = 1
gtk.GL_DEBUG_FUNC = 2
gtk_Func("widget_set_gl_capability", { ret=gboolean;
  "pointer" --[[GtkWidget*]], "pointer" --[[GdkGLConfig*]], "pointer" --[[GdkGLContext* share_list]], gboolean --[[direct]], "int" --[[render_type]] })
gtk_Func("widget_is_gl_capable", { ret=gboolean; "pointer" --[[GtkWidget*]] })
gtk_Func("widget_get_gl_config", { ret="pointer" --[[GdkGLConfig*]]; "pointer" --[[GtkWidget*]] })
gtk_Func("widget_create_gl_context", { ret="pointer" --[[GdkGLContext*]];
  "pointer" --[[GtkWidget*]], "pointer" --[[GdkGLContext* share_list]], gboolean --[[direct]], "int" --[[render_type]] })
gtk_Func("widget_get_gl_context", { ret="pointer" --[[GdkGLContext*]]; "pointer" --[[GtkWidget*]] })
gtk_Func("widget_get_gl_window", { ret="pointer" --[[GdkGLWindow*]]; "pointer" --[[GtkWidget*]] })

local GdkGLConfigMode = 'int'
gdk.GL_MODE_RGB = 0
gdk.GL_MODE_RGBA = 0
gdk.GL_MODE_INDEX = 1
gdk.GL_MODE_SINGLE = 0
gdk.GL_MODE_DOUBLE = 2
gdk.GL_MODE_STEREO = 4
gdk.GL_MODE_ALPHA = 8
gdk.GL_MODE_DEPTH = 16
gdk.GL_MODE_STENCIL = 32
gdk.GL_MODE_ACCUM = 64
gdk.GL_MODE_MULTISAMPLE = 128

gdk_Func("gl_config_new_by_mode", { ret="pointer" --[[GdkGLConfig*]]; GdkGLConfigMode })
gdk_Func("gl_drawable_gl_begin", { ret=gboolean; "pointer" --[[GdkGLDrawable*]], "pointer" --[[GdkGLContext*]] })
gdk_Func("gl_drawable_gl_end", { ret="void"; "pointer" --[[GdkGLDrawable*]] })

gdk_Func("gl_drawable_is_double_buffered", { ret=gboolean; "pointer" --[[GdkGLDrawable*]] })
gdk_Func("gl_drawable_swap_buffers", { ret="void"; "pointer" --[[GdkGLDrawable*]] })
